package org.wmq.javafxmapcollectioncs.mapper.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.wmq.javafxmapcollectioncs.mapper.MapEntityMapper;
import org.wmq.javafxmapcollectioncs.model.Entity.MapEntity;
import org.wmq.javafxmapcollectioncs.model.Entity.Result;

import java.io.IOException;

public class MapEntityMapperImplByHttpAndMysql implements MapEntityMapper {
    @Override
    public MapEntity selectById(Long id) {
        String url = "http://localhost:8080/mapentity/" + id;
        String resultJson = HttpUtil.get(url);
        Result<MapEntity> result = JSON.parseObject(resultJson, Result.class);
        return result.getData();
    }



    @Override
    public void insert(MapEntity mapEntity) {
        String url = "http://localhost:8080/mapentity";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(mapEntity.toString()));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                System.out.println(response.getVersion()); // HTTP/1.1
                System.out.println(response.getCode()); // 200
                System.out.println(response.getReasonPhrase()); // OK
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(MapEntity mapEntity) {
        String url = "http://localhost:8080/mapentity";
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(new StringEntity(mapEntity.toString()));
        httpPut.setHeader("Accept", "application/json");
        httpPut.setHeader("Content-type", "application/json");
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpPut)) {
                System.out.println(response.getVersion()); // HTTP/1.1
                System.out.println(response.getCode()); // 200
                System.out.println(response.getReasonPhrase()); // OK
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteById(Long id) {
        String url = "http://localhost:8080/mapentity/" + id;
        HttpDelete httpDelete = new HttpDelete(url);
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpDelete)) {
                System.out.println(response.getVersion()); // HTTP/1.1
                System.out.println(response.getCode()); // 200
                System.out.println(response.getReasonPhrase()); // OK
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}